/*
 *  DAO & service function 命名约束
 *  search*() 查询
 *  save*() 保存
 *  update*() 修改
 *  delete*() 删除
 */
package com.alter.global.base.article.dao;

import java.sql.Clob;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.alter.global.base.ArticleSearchVO;
import com.alter.model.AfArticle;
import com.alter.orm.Page;
import com.alter.orm.hibernate.HibernateDao;

@Repository
public class AfArticleDao extends HibernateDao<AfArticle, String> {
    
    /**
     * 保存文章.. 不得不将文件一直送到session层。
     * @param article
     * @param context
     */
    public void save(AfArticle article, String context) {
	Clob clob = getSession().getLobHelper().createClob(context);
	article.setContent(clob);
	save(article);
    }
    
    /**
     * 根据条件分页查询, 按时间降序排列
     * @param searchVO
     * @return
     */
    public Page<AfArticle> searchArticle(ArticleSearchVO searchVO) {
	Criteria criteria = getSession().createCriteria(AfArticle.class);
	if(StringUtils.isNotEmpty(searchVO.getType())){
	    Criteria type = criteria.createCriteria("bar");
	    type.add(Restrictions.eq("name",searchVO.getType()));
	}
	if(StringUtils.isNotEmpty(searchVO.getName())){
	    criteria.add(Restrictions.eq("creater", searchVO.getName()));
	}
	if(StringUtils.isNotEmpty(searchVO.getAtype())){
	    Criteria type = criteria.createCriteria("type");
	    type.add(Restrictions.eq("name",searchVO.getAtype()));
	}
	criteria.addOrder(Order.desc("create_date"));
	return findPage(searchVO, criteria);
    }
}
